using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RoboSmartIRobotCreate;
using System.Threading;

namespace IRobotCreateTestUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Robot iRobot = new Robot(true);

        private void button1_Click(object sender, EventArgs e)
        {
            iRobot.Connection.DataReceivedEventHandler += new Connection.DataReceivedDelegate(Connection_DataReceivedEventHandler);
            try
            {
                iRobot.Connection.ComPort = int.Parse(textBox1.Text);

                if (iRobot.Connection.Open())
                {
                    MessageBox.Show("Connected successfully");
                }

            }
            catch 
            {
                MessageBox.Show("Please give a number");
            }
                    }

        void Connection_DataReceivedEventHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            MessageBox.Show(iRobot.Connection.Available.ToString());
            byte [] buf = new byte[iRobot.Connection.Available];
            iRobot.Connection.ReadBytes(buf); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            iRobot.Connection.DataReceivedEventHandler -=new Connection.DataReceivedDelegate(Connection_DataReceivedEventHandler);
            iRobot.Connection.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                iRobot.Start((RobotMode)comboBox1.SelectedIndex);
            }
            else
            {
                iRobot.Start(RobotMode.Passive);
            }            
        }

       

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (string s in textBox2.Lines)
            {
                string[] linebytes = s.Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                List<byte> bytelist = new List<byte>();
                for (int i = 0; i < linebytes.Length; i++)
                {
                    bytelist.Add(byte.Parse(linebytes[i]));
                }
                iRobot.SendBytes(bytelist.ToArray());
            }
        }

        

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            label4.Text = string.Format("Battery: {0}%", iRobot.BatteryCharge);
        }

        private void btnRotateRight_Click(object sender, EventArgs e)
        {
            iRobot.Rotate90Degrees(Direction.Right);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            iRobot.Rotate90Degrees(Direction.Left);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                int velocity = int.Parse(tbVelocity.Text);
                int distance = int.Parse(tbDistance.Text);
                iRobot.DriveStraight(velocity, distance);
            }
            catch
            {
                MessageBox.Show("Please give numbers");
            }
        }

        private void btnPollSensors_Click(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex != -1)
            {
                if ((iRobot.PollTouchSensor((TouchSensor)
                    ((int)TouchSensor.BumpsAndWheelDrops + comboBox2.SelectedIndex)) & 1) == 1)
                {
                    lbSensorStatus.Text = "Pressed";
                    iRobot.DriveStraight(-100, -100);
                    iRobot.Rotate90Degrees(Direction.Left);
                }
            }
        }

        private void btnRandomDrive_Click(object sender, EventArgs e)
        {
            Random r = new Random((int)DateTime.Now.Ticks);

            for (int i = 0; i < 30; i++)
            {
                iRobot.DriveStraight(100, 100);
                // detected some touch
                Thread.Sleep(500);
                if (iRobot.PollTouchSensors() > 0)
                {
                    Thread.Sleep(500);
                    iRobot.DriveStraight(-100, -200);

                    if (r.Next() % 2 == 0)
                    {
                        iRobot.Rotate90Degrees(Direction.Left);
                    }
                    else
                    {
                        iRobot.Rotate90Degrees(Direction.Right);
                    }
                    Thread.Sleep(500);
                }
                
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int res = 0;
            for (int i = 0; i < 70; i++)
			{
			    res = iRobot.PollLightSensor();
                Thread.Sleep(100);
			}
            MessageBox.Show("Done!");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 30; i++)
            {
                int light = iRobot.PollLightSensor();
                Thread.Sleep(100);
                label9.Text = string.Format("Light: {0}", light);
                Thread.Sleep(100);
                if ((light <= 720) && (light >= 600))
                {
                    iRobot.DriveStraight(100, 50);
                }
                else
                {
                    iRobot.DriveRobot(100, 300, 50);
                }
                Thread.Sleep(100);

            }
        }



    }
}